<template>
  <div class="search-bar">
    <el-form :inline="true" :model="formItem" class="form-filter" size="mini">
      <el-form-item>
        <el-select v-model="formItem.s_unitCode" clearable filterable placeholder="厅局" class="fixed-width" @change="unitChange">
          <el-option v-for="(item) in availableInstitutions" :key="item.code" :label="item.name" :value="item.code" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="formItem.s_afEqReportDate"
          type="week"
          format="yyyy 第 WW 周"
          placeholder="从"
          style="width: 150px"
          @change="search"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="formItem.s_beEqReportDate"
          type="week"
          format="yyyy 第 WW 周"
          placeholder="至"
          style="width: 150px"
          @change="search"
        />
      </el-form-item>
      <el-form-item>
        <el-button plain icon="el-icon-search" @click="search" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { byUser as myInstitutions } from '@neu/dcom-ui/api/institutions'
// 定义一个查询表单原始项
const formItem = {
  s_unitCode: '',
  s_afEqReportDate: null,
  s_beEqReportDate: null
}
export default {
  name: 'TransferWeeklyTableFilter',
  data() {
    return {
      formItem: {
        ...formItem
      },
      availableInstitutions: []
    }
  },
  created() {
    myInstitutions().then(response => {
      const data = response.data
      const institutions = []
      data.forEach(it => {
        institutions.push({ code: it.code, name: it.name })
      })
      this.availableInstitutions = institutions
    })
  },
  methods: {
    // 查询按钮点击事件处理
    search: function() {
      const data = {
        ...this.formItem
      }
      if (data.s_afEqReportDate) {
        data.s_afEqReportDate = data.s_afEqReportDate.getTime()
      }
      if (data.s_beEqReportDate) {
        data.s_beEqReportDate = data.s_beEqReportDate.getTime()
      }
      this.$parent.filter(data)
    },
    // 重置按钮点击事件处理
    reset: function() {
      this.formItem = {
        ...formItem
      }
      this.search()
    },
    unitChange() {
      this.search()
    }
  }
}
</script>
